package com.hellobike.haxing.web.home;


import com.alibaba.fastjson.JSON;
import com.hellobike.haxing.anno.NoAuth;
import com.hellobike.haxing.operate.api.IOperateCmsPageService;
import com.hellobike.haxing.operate.api.IOperateTilingService;
import com.hellobike.haxing.operate.model.vo.OperateCmsPageVo;
import com.hellobike.haxing.operate.model.vo.OperateTilingVo;
import com.hellobike.haxing.order.api.IOrderAftersaleService;
import com.hellobike.haxing.order.api.IOrderBService;
import com.hellobike.haxing.order.result.Result;
import com.hellobike.haxing.response.home.CustomerData;
import com.hellobike.haxing.response.home.CustomerDetail;
import com.hellobike.haxing.response.home.HomeVo;
import com.hellobike.haxing.user.api.CustomerApi;
import com.hellobike.haxing.user.api.ShopApi;
import com.hellobike.haxing.user.api.ShopStaticsApi;
import com.hellobike.haxing.user.response.HomeShopResponse;
import com.hellobike.haxing.user.vo.*;
import com.hellobike.haxing.util.RedisUtil;
import com.hellobike.haxing.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "首页接口")
@RestController
@RequestMapping("/home")
@Slf4j
public class HomeController {
    @Reference(version = "1.0.0", check = true)
    private IOrderBService orderBService;
    @Reference(version = "1.0.0", check = true)
    private IOrderAftersaleService orderAftersaleService;
    @Reference(version = "1.0.0")
    private ShopApi shopApi;
    @Autowired
    private UserUtil userUtil;
    @Reference(version = "1.0.0")
    private ShopStaticsApi shopStaticsApi;

    @Autowired
    private RedisUtil redisUtil;

    @Reference(version = "1.0.0")
    private IOperateTilingService operateTilingService;

    private String url = "/pages/headTask/index";


    @Reference(version = "1.0.0")
    private CustomerApi customerApi;

    @Reference(version = "1.0.0")
    private IOperateCmsPageService operateCmsPageService;


    @ApiOperation("查询客服信息")
    @PostMapping(value = "/customer")
    public Result<CustomerData> customer() {
        Result<CustomerData> result = new Result<>();
        List<CustomerVo> customerVoList = customerApi.list();
        if (!CollectionUtils.isEmpty(customerVoList)) {
            CustomerData data = new CustomerData();
            data.setWechatNo(customerVoList.get(0).getWechatNo());
            data.setCustomerList(new ArrayList<>());
            CustomerDetail detail1 = new CustomerDetail();
            detail1.setText("第一步，点击复制客服微信号;");
            data.getCustomerList().add(detail1);
            CustomerDetail detail2 = new CustomerDetail();
            detail2.setText("第二步，点击添加好友;");
            detail2.setImg("https://xuqing-image.oss-cn-shanghai.aliyuncs.com/service_step2.png");
            CustomerDetail detail3 = new CustomerDetail();
            detail3.setText("第三步，粘贴客服微信号;");
            detail3.setImg("https://xuqing-image.oss-cn-shanghai.aliyuncs.com/service_step3.png");
            CustomerDetail detail4 = new CustomerDetail();
            detail4.setText("第四步，添加好友;");
            detail4.setImg("https://xuqing-image.oss-cn-shanghai.aliyuncs.com/service_step4.png");
            CustomerDetail detail5 = new CustomerDetail();
            detail5.setText("完成以上步骤，开始跟客服沟通;");
            data.getCustomerList().add(detail2);
            data.getCustomerList().add(detail3);
            data.getCustomerList().add(detail4);
            data.getCustomerList().add(detail5);
            result.setData(data);
        }
        return result;
    }

    @ApiOperation("首页平铺广告")
    @NoAuth
    @PostMapping(value = "/tiling")
    public Result<OperateTilingVo> tiling() {
        Result<OperateTilingVo> result = new Result<>();
        OperateTilingVo operateTilingVo = new OperateTilingVo();
        operateTilingVo.setLinkUrl(url);
        operateTilingVo.setHref("https://hxn-product-image.oss-cn-beijing.aliyuncs.com/icon/red-packge-banner.png");
        result.setData(operateTilingVo);
        return result;
    }

    @ApiOperation("首页显示")
    @RequestMapping(value = "/home", method = RequestMethod.POST)
//    @NoAuth
    public Result<HomeVo> home() {
        Result<HomeVo> result = new Result<>();
        HomeVo vo = new HomeVo();
        UserVo userVo = userUtil.getUser();
        if (userVo == null) {
            vo.setFakePage(true);
            result.success("").setData(vo);
            return result;
        }
        SmallShopVo shopVo = userVo.getShopVo();
        Long shopId = shopVo.getId();
        ShopStaticsVo shopStaticsVo = shopStaticsApi.getShopStaticDayCount(shopId);
        vo.setDayCount(shopStaticsVo.getOrderCount());
        vo.setShopRank(shopStaticsVo.getSort());
        vo.setDayCustomers(shopStaticsVo.getDayCustomers());
        vo.setDaySumIncomeAmount(shopStaticsVo.getCommissionAmount());
        vo.setExceptedAmount(shopStaticsVo.getExceptedAmount());
        HomeShopResponse response = shopApi.queryHomeShopInfo(shopId);
        LeaderStatiscVo leaderStatiscVo = shopStaticsApi.getPromoterStats(shopVo.getId());
        if (leaderStatiscVo != null) {
            vo.setDayPromoterCount(leaderStatiscVo.getDayPromoterCount());
            vo.setMonthPromoterCount(leaderStatiscVo.getMonthPromoterCount());
            vo.setPromoterCount(leaderStatiscVo.getPromoterCount());
        }
        vo.setShopVo(response.getShopVo());
        vo.setLeaderUser(response.getShopVo().getLeadShopName());
        vo.setLeaderName(response.getShopVo().getLeadShopName());
        vo.setSumIncomeAmount(response.getSumIncomeAmount());
        vo.setTotalAmount(response.getTotalAmount());
        if (shopVo.getShopType() != null && shopVo.getShopType() == 2) {

        }
        OperateCmsPageVo pageVo = operateCmsPageService.queryPlatformPage(2);
        log.info("page-info:{}", JSON.toJSONString(pageVo));
        if (pageVo != null) {
            vo.setCmsId(pageVo.getId());
        }
        result.success("").setData(vo);
        log.info("home-result:{}", JSON.toJSONString(vo));
        return result;
    }
}
